package com.yiqi.news.mvp.ui.fragment

import android.content.Intent
import android.os.Bundle
import android.os.Message
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import butterknife.OnClick

import com.jess.arms.base.BaseFragment
import com.jess.arms.di.component.AppComponent
import com.jess.arms.utils.ArmsUtils

import com.yiqi.news.di.component.DaggerNewsComponent
import com.yiqi.news.di.module.NewsModule
import com.yiqi.news.mvp.contract.NewsContract
import com.yiqi.news.mvp.presenter.NewsPresenter

import com.yiqi.news.R
import com.yiqi.news.app.EventBusTags
import com.yiqi.news.app.widget.SignDialog
import com.yiqi.news.app.widget.SignSuccessDialog
import com.yiqi.news.app.widget.qmui.QMUITipDialog
import com.yiqi.news.entity.SignHistoryVO
import com.yiqi.news.mvp.ui.activity.SearchActivity
import kotlinx.android.synthetic.main.fragment_news.*
import org.simple.eventbus.EventBus
import java.util.ArrayList

class NewsFragment : BaseFragment<NewsPresenter>(), NewsContract.View {

    private lateinit var mSignDialog: SignDialog

    private val mSignSuccessDialog: SignSuccessDialog by lazy {
        return@lazy SignSuccessDialog(context)
    }

    override fun showSignHistorySuccess(data: List<SignHistoryVO>) {
        mSignDialog= SignDialog(context,data)
        mSignDialog.setListener { mPresenter?.signIn() }
        mSignDialog.show()
    }

    override fun initView() {}

    override fun getTabNewsData(tabs: Array<String>, fragment: ArrayList<Fragment>) {
        tl_news.setFragmentViewPager(vp, tabs, this, fragment)
    }

    override fun initEvent() {

    }

    override fun lazyFetchData() {
    }

    companion object {
        fun newInstance(): NewsFragment {
            return NewsFragment()
        }
    }

    private val mQMUIInfoDialog: QMUITipDialog by lazy {
        return@lazy QMUITipDialog.Builder(mContext)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_SUCCESS)
                .setTipWord("签到失败")
                .create()
    }

    override fun setupFragmentComponent(appComponent: AppComponent) {
        DaggerNewsComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .newsModule(NewsModule(this))
                .build()
                .inject(this)
    }

    override fun initView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.fragment_news, container, false);
    }

    override fun initData(savedInstanceState: Bundle?) {
        mPresenter?.getTabData()
    }

    override fun setData(data: Any?) {

    }

    override fun signInSuccess() {
        mSignDialog.dismiss()
        mSignSuccessDialog.show()
        EventBus.getDefault().post(Message(), EventBusTags.NEED_UPDATE_DATA)
    }

    override fun signInError(msg: String) {
        mQMUIInfoDialog.show()
    }

    override fun showMessage(message: String) {
        ArmsUtils.snackbarText(message)
    }

    override fun launchActivity(intent: Intent) {
        ArmsUtils.startActivity(intent)
    }

    override fun killMyself() {
    }

    override fun initImmersionBar() {
        super.initImmersionBar()
        mImmersionBar.statusBarDarkFont(true).init()
        mImmersionBar.titleBar(rl_title).init()
    }

    override fun useImmersionBar(): Boolean? = true

    @OnClick(R.id.iv_news_search, R.id.iv_news_calender)
    fun onClick(view: View) {
        when (view.id) {
            R.id.iv_news_search -> ArmsUtils.startActivity(SearchActivity::class.java)
            R.id.iv_news_calender -> mPresenter?.getSignHistory()
        }
    }
}
